﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UCCheckBox
{
    [DefaultEvent("CheckedChangeEvent")]
    public partial class CheckBox : UserControl
    {
        [Description("选中改变事件"), Category("自定义")]
        public event EventHandler CheckedChangeEvent;

        [Description("字体"), Category("自定义")]
        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
                label1.Font = value;
            }
        }

        private Color _ForeColor = Color.FromArgb(62, 62, 62);
        [Description("字体颜色"), Category("自定义")]
        public new Color ForeColor
        {
            get { return _ForeColor; }
            set
            {
                label1.ForeColor = value;
                _ForeColor = value;
            }
        }

        private string _Text = "复选框";
        [Description("文本"), Category("自定义")]
        public string TextValue
        {
            get { return _Text; }
            set
            {
                label1.Text = value;
                _Text = value;
            }
        }

        private bool _checked = false;
        [Description("是否选中"), Category("自定义")]
        public bool Checked
        {
            get { return _checked; }
            set
            {
                if(_checked!=value)
                {
                    _checked = value;
                    if(base.Enabled)
                    {
                        if(_checked)
                        {
                            pictureBox1.Image = AESEncrypt.Properties.Resources.checkbox1;
                        }
                        else
                        {
                            pictureBox1.Image = AESEncrypt.Properties.Resources.checkbox0;
                        }
                    }
                    else
                    {
                        if(_checked)
                        {
                            pictureBox1.Image = AESEncrypt.Properties.Resources.checkbox10;
                        }
                        else
                        {
                            pictureBox1.Image = AESEncrypt.Properties.Resources.checkbox00;
                        }
                    }
                    if(CheckedChangeEvent!=null)
                    {
                        CheckedChangeEvent(this, null);
                    }
                }
            }
        }

        public new bool Enabled
        {
            get { return base.Enabled; }
            set
            {
                base.Enabled = value;
                if(value)
                {
                    if(_checked)
                    {
                        pictureBox1.Image = AESEncrypt.Properties.Resources.checkbox1;
                    }
                    else
                    {
                        pictureBox1.Image = AESEncrypt.Properties.Resources.checkbox0;
                    }
                }
                else
                {
                    if(_checked)
                    {
                        pictureBox1.Image = AESEncrypt.Properties.Resources.checkbox10;
                    }
                    else
                    {
                        pictureBox1.Image = AESEncrypt.Properties.Resources.checkbox00;
                    }
                }
            }
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            int x = 5;
            int y = (int)(0.5 * (this.Height - pictureBox1.Height));
            pictureBox1.Location = new Point(x, y);
            x = 5 + pictureBox1.Width + 5;
            y = (int)(0.5 * (this.Height - label1.Height));
            label1.Location = new Point(x, y);
        }

        public CheckBox()
        {
            InitializeComponent();
            label1.Text = "复选框";
            pictureBox1.Image = AESEncrypt.Properties.Resources.checkbox0;
            pictureBox1.Show();
            this.MinimumSize = new Size(60, 16);
            this.AutoSize = true;
        }

        private void CheckBox_MouseDown(object sender, MouseEventArgs e)
        {
            Checked =! Checked;           
        }
    }
}
